package com.scommunity.common;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 全局统一响应
 * @param <T>
 */
@Data
@AllArgsConstructor
public class ResponseResult<T> {
    private int code;
    private String msg;

    private T data;

    private static final int OK_CODE = 200;
    private static final String OK_MSG = "成功";



    //默认为成功响应
    private ResponseResult() {
        this(OK_CODE,OK_MSG);
    }

    public ResponseResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 响应成功不带数据
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> success() {
        return new ResponseResult<>(OK_CODE,OK_MSG);
    }

    /**
     * 响应成功带数据
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(OK_CODE,OK_MSG,data);
    }

    /**
     * 响应错误不带数据
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> error(Integer code,String msg) {
        return new ResponseResult<>(code,msg);
    }

    /**
     * 响应错误带数据
     * @param code
     * @param msg
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> error(Integer code,String msg, T data) {
        return new ResponseResult<>(code,msg,data);
    }

}
